#include "stats.h"

stats* stats::createStats(actor *parent,const char* name, float max_hp_, float max_mana_, float damage_, float mana_regen_)
{
    stats *s = new stats();
    s->parent_ = parent;
    s->hp = max_hp_;
    s->max_hp = max_hp_;
    s->mana = max_mana_;
    s->max_mana = max_mana_;
    s->damage = damage_;
    s->mana_regen = mana_regen_;
    s->name_ = name;
    if(parent)
        parent->addChild(s);
    return s;
}

void stats::update(float dt)
{
    node::update(dt);
    regenMana(dt);
    if (is_invincible_) {
        invincible_timer_ += dt;
        if (invincible_timer_ >= invincible_time_) {
            is_invincible_ = false;
            invincible_timer_ = 0;
        }
    }
}

void stats::useMana(float cost)
{
    mana -= cost;
    if(mana < 0) mana = 0;
}

void stats::regenMana(float dt)
{
    mana += mana_regen * dt;
    if (mana > max_mana) 
        mana = max_mana;
}

void stats::takeDamage(float damage_)
{    
    if (is_invincible_) return;
    hp -= damage_;
    if (hp <= 0) {
        hp = 0;
        is_alive_ = false;
    }
    SDL_Log("%s Hp:%f",name_.c_str(),hp);
    is_invincible_ = true;
    invincible_timer_ = 0.0f;
}
